<?php

/**
 * 前台 新闻 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: show.class.php 48 2011-07-18 09:57:45Z zwt007 $
 * http://www.t-mac.org；
 */
class show extends Model
{

    private $article_table;
    private $category_table;
    private $addonarticle_table;
    private $addonimages_table;
    private $add_category_table;
    private $index_model;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->article_table = '#@__article';
        $this->category_table = '#@__category';
        $this->addonarticle_table = '#@__addonarticle';
        $this->addonimages_table = '#@__addonimages';
        $this->add_category_table = '#@__addoncategory';
        $this->index_model = Tmac::model('index');
    }

    /**
     * 增加一次click
     * @param <type> $id
     * @return <type> 
     */
    public function saveArticleClick($id)
    {
        $sql = "UPDATE $this->article_table SET click_count = click_count+1 WHERE article_id = '$id'";
        $rs = $this->db->execute($sql);
        return $rs;
    }

    /**
     * 取栏目信息
     * @param <type> $nicename
     * @return <type>
     */
    public function getCategoryInfo($nicename, $uid)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->category_table WHERE category_nicename = '$nicename' LIMIT 0,1");
        if ($uid > 1) {
            $class_id = $rs['cat_id'];
            $add_cate_rs = $this->db->getRow("SELECT * FROM $this->add_category_table WHERE cat_id = $class_id AND uid=$uid LIMIT 0,1");
            if ($add_cate_rs) {
                if (!empty($add_cate_rs['cat_name'])) {
                    $rs['cat_name'] = $add_cate_rs['cat_name'];
                }
                if (!empty($add_cate_rs['cat_pid'])) {
                    $rs['cat_pid'] = $add_cate_rs['cat_pid'];
                }
                $rs['category_keywords'] = $add_cate_rs['category_keywords'];
                $rs['category_description'] = $add_cate_rs['category_description'];
//                $rs['category_content'] = $add_cate_rs['content'];
                $rs['urlfile'] = $add_cate_rs['urlfile'];
                $rs['category_content'] = str_replace('Public/uploadfiles/', STATIC_URL . 'Public/uploadfiles/', $add_cate_rs['content']);
            }
        }
        return $rs;
    }

    /**
     * 取栏目信息
     * @param <type> $nicename
     * @return <type>
     */
    public function getCategoryInfoById($cat_id, $uid)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->category_table WHERE cat_id = $cat_id LIMIT 0,1");
        if ($uid > 1) {
            $add_cate_rs = $this->db->getRow("SELECT * FROM $this->add_category_table WHERE cat_id = $cat_id AND uid=$uid LIMIT 0,1");
            if ($add_cate_rs) {
                if (!empty($add_cate_rs['cat_name'])) {
                    $rs['cat_name'] = $add_cate_rs['cat_name'];
                }
                if (!empty($add_cate_rs['cat_pid'])) {
                    $rs['cat_pid'] = $add_cate_rs['cat_pid'];
                }
                $rs['category_keywords'] = $add_cate_rs['category_keywords'];
                $rs['category_description'] = $add_cate_rs['category_description'];
//                $rs['category_content'] = $add_cate_rs['content'];
                $rs['urlfile'] = $add_cate_rs['urlfile'];
                $rs['category_content'] = str_replace('Public/uploadfiles/', STATIC_URL . 'Public/uploadfiles/', $add_cate_rs['content']);
            }
        }
        return $rs;
    }

    /**
     * 取出资讯无分页
     * @param <type> $cat_id
     * @return <type>
     */
    public function getArticleListNoPage($cat_id, $uid, $limit = 4)
    {
        $sql = "SELECT a.article_id , a.title, a.url, a.time, a.attachments, b.content FROM $this->article_table a LEFT JOIN $this->addonarticle_table b ON a.article_id = b.article_id WHERE a.type='post' AND a.cat_id = '$cat_id' AND a.uid = $uid AND (a.`show`=1) ORDER BY orderby DESC, article_id DESC";
        $sql .= " LIMIT 0,$limit";

        $rs = $this->db->getAll($sql);
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['time'] = date('Y-m-d', $v['time']);
            }
        }

        return $rs;
    }

    /**
     * 取出酒店无分页
     * @param <type> $cat_id
     * @return <type>
     */
    public function getHotelListNoPage($uid, $limit=5)
    {
        $sql = "SELECT a.article_id, a.title, a.url, b.star, b.address FROM $this->article_table a LEFT JOIN $this->addonimages_table b ON a.article_id = b.article_id WHERE a.channel = 4 AND a.uid = $uid AND (a.`show`=1) ORDER BY a.orderby DESC, a.article_id DESC LIMIT 0,$limit";
        $rs = $this->db->getAll($sql);

        $ErrorMsg = '';
        $count = count($rs);
        if ($count == 0) {
            $ErrorMsg = "暂无酒店! ";
        }

        $this->assign('rs', $rs);
        $this->assign('hotel_list_nopage_error', $ErrorMsg);
        return $rs;
    }

    /**
     * 取出酒店无分页
     * @param <type> $cat_id
     * @return <type>
     */
    public function getAnliListNoPage($uid, $limit=5, $cat_id=0)
    {
        $sql = "SELECT a.article_id, a.title, a.url, a.attachments, b.star, b.address FROM $this->article_table a LEFT JOIN $this->addonimages_table b ON a.article_id = b.article_id WHERE a.channel = 5 AND a.uid = $uid AND (a.`show`=1) AND (a.orderby='1') ";
        if (!empty($cat_id)) {
            $sql .= " AND (a.cat_id={$cat_id}) ";
        }
        $sql .= "ORDER BY a.orderby DESC, a.article_id DESC LIMIT 0,$limit";
        $rs = $this->db->getAll($sql);
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['attachments'] = unserialize($v['attachments']);
            }
        }
        return $rs;
    }

    /**
     * 取资讯无分页
     * @param type $cat_id
     * @param type $uid
     * @param type $limit
     * @return type 
     */
    public function getArticleNewsListNopage($cat_id, $uid, $limit = 5)
    {
        $cat_ids = $this->index_model->getSonTreeList($cat_id, $uid);
        $where = "WHERE (uid = $uid OR uid = 1) AND cat_id IN($cat_ids) AND (`show`=1)";
        $sql = "SELECT article_id, title FROM {$this->article_table} {$where} ORDER BY orderby DESC, article_id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

    /**
     * 取资讯无分页
     * @param type $cat_id
     * @param type $uid
     * @param type $limit
     * @return type 
     */
    public function getArticleNewsListNopageB($cat_id, $uid, $limit = 5)
    {
        $where = "WHERE (uid = $uid OR uid = 1) AND cat_id IN($cat_id) AND (`show`=1)";
        $sql = "SELECT article_id, title FROM {$this->article_table} {$where} ORDER BY orderby DESC, article_id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

}